package j2.shopmail;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import j2.shopmail.ui.notifications.NotificationsFragment;

import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class MainActivity extends AppCompatActivity
{
  public   static Context Self;
   public static  User user=new User();
    ProgressBar progressBar;
    AsycHttp<String,String> http;
    static NavController navController;
    class RegisterAction implements AsycHttpAction<String,String>
    {
        
        @Override
        public String BackGroundAction(String... params)
        {

           String c= ServerHelper.MakeRegisterStr(params[0],params[1]);
           http.PushProgress(20);
            String r = HttpHelper.Get(c);
            http.PushProgress(50);
            
            return HttpHelper.Get(ServerHelper.MakeParseStr(r));
        }
        
        @Override
        public void ProgressUpdateAction(View UI, Double... values)
        {
        progressBar.setProgress(values[0].intValue());
        }
        
        @Override
        public void ResultFunction(String result)
        {
            progressBar.setVisibility(View.INVISIBLE);
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
        }
        
        @Override
        public void PreDoFunction()
        {
            progressBar.setVisibility(View.VISIBLE);
        }
    }
    class LoginAction implements  AsycHttpAction<String,String>
    {
    
        @Override
        public String BackGroundAction(String... params)
        {
        
            String c= ServerHelper.MakeLoginStr(params[0],params[1]);
            user.name=params[0];
            user.password=params[1];
            http.PushProgress(20);
            String r = HttpHelper.Get(c);
            http.PushProgress(50);
        
            return HttpHelper.Get(ServerHelper.MakeParseStr(r));
        }
    
        @Override
        public void ProgressUpdateAction(View UI, Double... values)
        {
            progressBar.setProgress(values[0].intValue());
        }
    
        @Override
        public void ResultFunction(String result)
        {
            progressBar.setVisibility(View.INVISIBLE);
            if(result!=null)
            {
                if(result.equals("登录成功\r\n"))
                {
                    user.isLogin=true;

                    Switch(R.id.usercenter_fragment);
                }
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
            }

        }
    
        @Override
        public void PreDoFunction()
        {
            progressBar.setVisibility(View.VISIBLE);
        }
    }
    
    
    public void LoginClick(View v)
    {
        http=new AsycHttp<>();
        http.setBackgroundAction(new LoginAction());
        String n=  NotificationsFragment.username.getText().toString();
        String p=NotificationsFragment.password.getText().toString();
        if(n.length()>0&&p.length()>0)
            http.execute(n,p);
        else
            Toast.makeText(MainActivity.this, "请输入必要信息", Toast.LENGTH_SHORT).show();
    }
    public void RegClick(View v)
    {
        http=new AsycHttp<>();
        http.setBackgroundAction(new RegisterAction());
        String n=  NotificationsFragment.username.getText().toString();
        String p=NotificationsFragment.password.getText().toString();
        if(n.length()>0&&p.length()>0)
        http.execute(n,p);
        else
            Toast.makeText(MainActivity.this, "请输入必要信息", Toast.LENGTH_SHORT).show();
        
    }
    public void ForgotClick(View v)
    {
        if(NotificationsFragment.username.getText().toString().length()>6)
        {
     String name=NotificationsFragment.username.getText().toString();
     user.name=name;
     Switch(R.id.changepassword_fra);
        }
        else
        Toast.makeText(MainActivity.this, "请在用户名的输入框中输入您的用户名", Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Self=MainActivity.this;
        handleSSLHandshake();
        progressBar=findViewById(R.id.progressBar2);
        progressBar.setVisibility(View.INVISIBLE);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments)
            {
        if(destination.getId()==R.id.navigation_notifications)
        {
        if(user.isLogin)
        {
            controller.navigate(R.id.usercenter_fragment);
        }
        }
            }
        });
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        
    }
    public  static void Switch(int fragmentID)
    {
        navController.navigate(fragmentID);//切换页面
    }
    public static void handleSSLHandshake() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
                
                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                
                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }};
            
            SSLContext sc = SSLContext.getInstance("TLS");
            // trustAllCerts信任所有的证书
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } catch (Exception ignored) {
        }
    }
    
}
